<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\OrderCheckpointRefundRepository;
use HiCommon\Repository\OrderCheckpointVehicleRepository;
use HiCommon\Repository\OrderPayWechatRepository;

class OrderCheckpointRefundService extends BaseService
{

    /**
     * 年检预约订单 - 退款申请列表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param $arr_search
     *
     * @return array
     */
    public function api_apply_refund_waiting_list($arr_search){
        $arr_where = ['apply_refund_state'=>1];
        $arr_option = [
          'with'=>['order_checkpoint','njyy_vehicle']
        ];
        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        $arr_search['search_type'] = str_trim($arr_search['search_type']);
        $arr_search['search_order_type'] = str_trim($arr_search['search_order_type']);

        $data = OrderCheckpointRefundRepository::get_search_list($arr_where,$arr_search,$arr_option);
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }

    /**
     * 年检预约订单 - 已拒绝申请列表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param $arr_search
     *
     * @return array
     */
    public function api_apply_refund_refused_list($arr_search){
        $arr_where = ['apply_refund_state'=>9];
        $arr_option = [
          'with'=>['order_checkpoint','njyy_vehicle']
        ];
        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        $arr_search['search_type'] = str_trim($arr_search['search_type']);
        $arr_search['search_order_type'] = str_trim($arr_search['search_order_type']);

        $data = OrderCheckpointRefundRepository::get_search_list($arr_where,$arr_search,$arr_option);
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }


    /**
     * 年检预约订单 - 待退款列表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param $arr_search
     *
     * @return array
     */
    public function api_refund_waiting_list($arr_search){

        $arr_where = ['apply_refund_state'=>2];
        $arr_option = [
          'with'=>['order_checkpoint','njyy_vehicle']
        ];

        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        $arr_search['search_type'] = str_trim($arr_search['search_type']);
        $arr_search['search_order_type'] = str_trim($arr_search['search_order_type']);

        $data = OrderCheckpointRefundRepository::get_search_list($arr_where,$arr_search,$arr_option);
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }

    /**
     * 年检预约订单 - 已退款列表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param $arr_search
     *
     * @return array
     */
    public function api_refunded_list($arr_search){

        $arr_where = ['apply_refund_state'=>3];
        $arr_option = [
          'with'=>['order_checkpoint','njyy_vehicle']
        ];

        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        $arr_search['search_type'] = str_trim($arr_search['search_type']);
        $arr_search['search_order_type'] = str_trim($arr_search['search_order_type']);

        $data = OrderCheckpointRefundRepository::get_search_list($arr_where,$arr_search,$arr_option);
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }





    /**
     * 同意退款申请-等待退款
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param int $n_id
     *
     * @return array
     */
    public function api_apply_refund_agree($n_id = 0){
        $n_id = intval($n_id);
        if($n_id <= 0 ){
            return hello_error('数据ID不能为空');
        }

        //查询申请数据
        $arr_order_checkpoint_refund = OrderCheckpointRefundRepository::get_one($n_id)->toarray();
        $m_apply_refund_state = $arr_order_checkpoint_refund['apply_refund_state'];
        if($m_apply_refund_state != 1 ){
            return hello_error('状态异常，无法操作');
        }

        //申请通过 1申请表状态修改 2车辆子表状态修改

        //1申请表状态修改
        $n_apply_refund_state = 2;
        $s_date_time = time_to_ymdhis();
        $arr_where = ['id'=>$n_id];
        $arr_data = [
          'apply_refund_state'=>$n_apply_refund_state,
          'deal_at'=>$s_date_time
        ];
        $res = OrderCheckpointRefundRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('操作失败');
        }

        //2车辆子表状态修改 待退款+检测失败
        $arr_where = ['id'=>$arr_order_checkpoint_refund['order_checkpoint_vehicle_id']];
        $arr_data = [
          'apply_refund_state'=>$n_apply_refund_state,
//          'check_state'=>9,
//          'checked_at'=>$s_date_time
        ];
        $res = OrderCheckpointVehicleRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('操作失败1');
        }
        return  hello_success('操作成功');
    }


    /**
     * 拒绝退款申请
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     *
     * @param int    $n_id
     * @param string $s_refuse_reason
     *
     * @return array
     */
    public function api_apply_refund_refuse($n_id = 0,$s_refuse_reason = ''){
        $n_id = intval($n_id);
        $s_refuse_reason = str_trim($s_refuse_reason);
        if($n_id <= 0 ){
            return hello_error('数据ID不能为空');
        }

        //查询申请数据
        $arr_order_checkpoint_refund = OrderCheckpointRefundRepository::get_one($n_id)->toarray();
        if($arr_order_checkpoint_refund['apply_refund_state'] != 1 ){
            return hello_error('状态异常，无法操作');
        }

        //申请通过 1申请表状态修改 2车辆子表状态修改

        //1申请表状态修改
        $n_apply_refund_state = 9;
        $arr_where = ['id'=>$n_id];
        $arr_data = [
          'apply_refund_state'=>$n_apply_refund_state,
          'refuse_reason'=>$s_refuse_reason,
          'deal_at'=>time_to_ymdhis()
        ];
        $res = OrderCheckpointRefundRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('操作失败');
        }

        //2车辆子表状态修改
        $arr_where = ['id'=>$arr_order_checkpoint_refund['order_checkpoint_vehicle_id']];
        $arr_data = [
          'apply_refund_state'=>$n_apply_refund_state,
        ];
        $res = OrderCheckpointVehicleRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('操作失败');
        }
        return  hello_success('操作成功');

    }


//    public function refund_logic($n_id,&$refund_data){
//        $n_id = intval($n_id);
//        if($n_id <= 0 ){
//            return hello_error('数据ID不能为空',[],'售后ID不能为空');
//        }
//
//        //查询申请数据
//        $arr_option = ['with'=>['order_checkpoint']];
//        $arr_order_checkpoint_refund = OrderCheckpointRefundRepository::base_find_one($n_id,$arr_option)->toarray();
//        if($arr_order_checkpoint_refund['apply_refund_state'] != 2 ){
//            return hello_error('状态异常，无法操作');
//        }
//        $arr_order = $arr_order_checkpoint_refund['order_checkpoint'];
//        $s_out_trade_no = $arr_order['order_sn'];
//        $s_refund_no = $arr_order_checkpoint_refund['refund_sn'];
//        $n_refund_fee = intval($arr_order_checkpoint_refund['refund_price']*100);
//        $refund_data = [
//          'out_trade_no'=>$s_out_trade_no,
//          'refund_no'=>$s_refund_no,
//          'refund_fee'=>$n_refund_fee,
//        ];
//
//        //申请设置为已退款状态 车辆子表和退款申请表
//        $n_apply_refund_state = 3;
//        $s_refund_time = time_to_ymdhis();
//        if($arr_order_checkpoint_refund['order_checkpoint_vehicle_id'] > 0){
//
//            //单独设置一个为已退款
//            $arr_where = ['id'=>$arr_order_checkpoint_refund['order_checkpoint_vehicle_id']];
//            $arr_data = [
//              'apply_refund_state'=>$n_apply_refund_state,
////              'check_state'=>9,
////              'checked_at'=>$s_refund_time
//            ];
//            $res = OrderCheckpointVehicleRepository::edit_by_where($arr_where,$arr_data);
//            if($res === false){
//                return hello_error('退款失败',[],'车辆子单数据修改失败');
//            }
//        }
//
//
//        $arr_where = ['id'=>$n_id];
//        $arr_data = ['apply_refund_state'=>$n_apply_refund_state,'refund_time'=>$s_refund_time];
//        $res = OrderCheckpointRefundRepository::edit_by_where($arr_where,$arr_data);
//        if($res === false){
//            return hello_error('退款失败',[],'退款申请数据修改失败');
//        }
//
//        //主表联动操作
//        $res = OrderCheckpointService::deal_fail_single_other($arr_order['id'],$s_refund_time);
//
//        return $res;
//    }

//    /**
//     * 退款处理
//     *
//     * @param $n_id
//     *
//     * @return array
//     * @throws \Yansongda\Pay\Exceptions\GatewayException
//     * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
//     * @throws \Yansongda\Pay\Exceptions\InvalidConfigException
//     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function api_deal_refund($n_id){
//
////        //处理订单逻辑
////        $res = OrderCheckpointRefundService::refund_logic($n_id,$arr_refund_data);
////        if($res['state'] != 0){
////            return $res;
////        }
//
//        //退款并记录退款数据
//        $s_out_trade_no = $arr_refund_data['out_trade_no'];
//        $s_refund_no = $arr_refund_data['refund_no'];
//        $n_refund_fee = $arr_refund_data['refund_fee'];
//        $result_refund = PayService::online_pay_refund($s_out_trade_no,$s_refund_no,$n_refund_fee);
//
//        return $result_refund;
//    }

}